//
//  BuyerSeacherViewController.m
//  XinKaiFa55like
//
//  Created by 55like on 2017/9/25.
//  Copyright © 2017年 55like lj. All rights reserved.
//

#import "BuyerSeacherViewController.h"
#import "CRMListTableViewCell.h"
#import "YYModel.h"
#import "BuyerSeacherClassViewController.h"
#import "BuyerAndSellerDetailViewController.h"



@interface BuyerSeacherViewController ()<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView * zhltableView;
@property (nonatomic,strong)NSMutableArray * dataArray;
@property (nonatomic,strong)NSMutableArray * menuArray;
@property (nonatomic,assign)NSInteger index1;
@property (nonatomic,assign)NSInteger index2;
@end

@implementation BuyerSeacherViewController
{
    
    UITextField * textfield;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navView =  [self navbarTitle:@""];
    self.index1 = 0;
    self.index2 = 0;
    UIView*viewSeach=[RHMethods viewWithFrame:CGRectMake(10, 0, kScreenWidth-60, 30) backgroundcolor:zrgbwhiteColor superView:self.navView];
    viewSeach.frameBY=7;
    viewSeach.layer.borderColor=rgb(229,229,229).CGColor;
    viewSeach.layer.borderWidth=1;
    viewSeach.layer.cornerRadius=3;
    UIImageView*imgVicon=[RHMethods imageviewWithFrame:CGRectMake(10, 8, 16, 16) defaultimage:@"xkf_search" supView:viewSeach];
    
    UITextField*tfSearch=[RHMethods textFieldlWithFrame:CGRectMake(imgVicon.frameXW+8, 0, viewSeach.frameWidth-imgVicon.frameXW-8-8, viewSeach.frameHeight) font:Font(13) color:nil placeholder:@"请输入公司名称等关键字" text:@""];
    tfSearch.delegate = self;
    [tfSearch setReturnKeyType:UIReturnKeySearch];
    [viewSeach addSubview:tfSearch];
    textfield = tfSearch;
    
    self.navrightButton = [self rightButton:@"取消" image:nil sel:@selector(backButtonClicked:)];
    [self.navrightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.menuArray = [[NSMutableArray alloc]init];
    
    [self.menuArray addObject:@{@"title":@"所有",@"id":@"0"}];
    [self.menuArray addObject:@{@"title":@"所有",@"id":@"0"}];
    

    self.dataArray = [[NSMutableArray alloc]init];
    [self.zhltableView reloadData];
}


//数据刷新
-(void)makeData{
    
    NSString * cid = [self.menuArray[0] valueForJSONStrKey:@"id"];
    NSString * issuccess = [self.menuArray[1] valueForJSONStrKey:@"id"];
 
    krequestParam
    [dictparam setValue:textfield.text forKey:@"key"];
    [dictparam setValue:cid forKey:@"cid"];
    [dictparam setValue:issuccess forKey:@"issuccess"];
    [NetEngine createHttpAction:@"customer/searchcustomerlist" withCache:NO withParams:dictparam withMask:SVProgressHUDMaskTypeClear onCompletion:^(id resData, BOOL isCache) {
        if ([[resData valueForJSONStrKey:@"status"] isEqualToString:@"200"]) {
            
            self.dataArray = [[NSMutableArray alloc]init];
            NSDictionary * dic = resData[@"data"];
            NSArray * arr = [dic[@"list"] mutableCopy];
            for (int i = 0; i < arr.count; i++) {
                
                NSDictionary * data = arr[i];
                ListModel * model = [ListModel yy_modelWithDictionary:data];
                
                [self.dataArray addObject:model];
                
            }
            
            [self.zhltableView reloadData];
            
            
        }else{
            
            
            [self.zhltableView reloadData];
            
            
            
        }
        
    } onError:^(NSError *error) {
        
        
        [self.zhltableView reloadData];
        
    }];
    
    
    
    
}

-(UITableView *)zhltableView{
    
    if (!_zhltableView) {
        
        UITableView * zhltableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64) style:UITableViewStyleGrouped];
        zhltableView.delegate = self;
        zhltableView.dataSource = self;
        zhltableView.separatorInset=UIEdgeInsetsMake(0,0, 0, 0);
        zhltableView.backgroundColor = [UIColor whiteColor];
        zhltableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        zhltableView.sectionHeaderHeight = 0;
        zhltableView.sectionFooterHeight = 0;
        zhltableView.showsVerticalScrollIndicator = NO;
        zhltableView.showsHorizontalScrollIndicator = NO;
        [self.view addSubview:zhltableView];
        [zhltableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
        [zhltableView registerClass:[CRMListTableViewCell class] forCellReuseIdentifier:@"CRMListTableViewCellID"];
        
        _zhltableView = zhltableView;
    }
  
    
    return _zhltableView;
}

#pragma mark -  tableview代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    
    return 2;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return 2;
    }
    
    return self.dataArray.count;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        
        return 10+12+8+12+10;
    }
    
    return 55;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    
    return CGFLOAT_MIN;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section    {
    
    
    
    return CGFLOAT_MIN;
}


#pragma mark  cell处理
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        NSMutableDictionary * dic = self.menuArray[indexPath.row];
        cell.textLabel.text = [dic valueForJSONStrKey:@"title"];
        if (indexPath.row == 0) {
            
            
            if ([[dic valueForJSONStrKey:@"title"] isEqualToString:@"所有"]) {
                
                cell.textLabel.text = @"分类";
                
            }
            
        }else{
            
            if ([[dic valueForJSONStrKey:@"title"] isEqualToString:@"所有"]) {
                
                cell.textLabel.text = @"状态";
                
            }
            
        }
        cell.textLabel.textColor = rgbTitleDeepGray;
        
        return cell;

    }
    
    //人物相信cell
    CRMListTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"CRMListTableViewCellID" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor whiteColor];
    ListModel * model = self.dataArray[indexPath.row];
    cell.model = model;
    return cell;
    
    
    
}
#pragma mark  选中cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSLog(@"%@",indexPath);
    EMWeakSelf(weakSelf);
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            
            
            [self classData];
            
        }else{
            
            NSArray * arr = @[@{@"title":@"成功",@"id":@"1"},@{@"title":@"未成过",@"id":@"2"}];
            
            NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
            [dic setValue:@"id" forKey:@"id"];
            [dic setValue:@"title" forKey:@"title"];
            [dic setValue:[NSString stringWithFormat:@"%ld",self.index2] forKey:@"index"];
            [dic setValue:^(NSDictionary * data,NSInteger index){
                
                
                weakSelf.index2 = index;
                [weakSelf.menuArray replaceObjectAtIndex:1 withObject:data];
                [weakSelf makeData];
                
            } forKey:@"block"];
            [self pushController:[BuyerSeacherClassViewController class] withInfo:arr withTitle:@"状态" withOther:dic];
            
        }
        
        
        
        
    }else{
        
        ListModel * model = self.dataArray[indexPath.row];
        NSMutableDictionary*mdic=[NSMutableDictionary new];
        [mdic setObject:model.z_id forKey:@"id"];
        
        [self pushController:[BuyerAndSellerDetailViewController class] withInfo:@"买家" withTitle:@"买家" withOther:mdic];
        
        
        
    }
    
    
    
}
-(void)classData{
    EMWeakSelf(weakSelf);
    krequestParam
    [NetEngine createHttpAction:@"customer/customercate" withCache:NO withParams:dictparam withMask:SVProgressHUDMaskTypeClear onCompletion:^(id resData, BOOL isCache) {
      
        if ([[resData valueForJSONStrKey:@"status"] isEqualToString:@"200"]) {
            
            NSDictionary * dic = resData[@"data"];
            NSArray * arr = dic[@"cate"];
            NSMutableArray * mArr = [[NSMutableArray alloc]init];
            for (int i = 0; i < arr.count; i ++) {
                
                NSDictionary * data = arr[i];
                [mArr addObject:data];
                
             
            }
            
            NSMutableDictionary * other = [[NSMutableDictionary alloc]init];
            [other setValue:@"id" forKey:@"id"];
            [other setValue:@"name" forKey:@"title"];
            [other setValue:[NSString stringWithFormat:@"%ld",self.index1] forKey:@"index"];
            [other setValue:^(NSDictionary * data,NSInteger index){
                
                weakSelf.index1 = index;
                [weakSelf.menuArray replaceObjectAtIndex:0 withObject:data];
                [weakSelf makeData];
                
            } forKey:@"block"];
            [self pushController:[BuyerSeacherClassViewController class] withInfo:mArr withTitle:@"分类" withOther:other];
            
        }else{
            
            
              [SVProgressHUD showImage:nil status:[resData valueForJSONStrKey:@"info"]];
            
            
            
        }
        
    } onError:^(NSError *error) {
        
    }];
    
    
}

#pragma mark -  textfield代理方法

//已经结束编辑
-(void)textFieldDidEndEditing:(UITextField *)textField{
    
    
    [self makeData];
    
}
//return键触发方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [self.view endEditing:YES];
    
    return YES;
}


@end
